<?php
namespace api\modules\v1\controllers;


use common\models\Member;
use common\models\SmsModel;
use think\Cache;
use Yii;

/*
	* 会员登录 注册 相关控制器
*/

class ConnectController extends CoreController
{

	public function actionTest()
	{
		echo  Yii::$app->security->generateRandomString();
	}


	/*
		*会员手机号码注册
	*/
	public function actionReg()
	{
		$this->write_log($this->request,'reg');
		$this->request['create_time'] = time();
		if (strlen($this->request['member_mobile'])!=11){
            $this->error('请填写正确的手机号码!');
        }
		$model = $this->model('member\Member',$this->request);     
		$model->loginpwd = md5($model->loginpwd);
		$model->paypwd = md5($model->paypwd);
		$result = Member::getOne(['member_mobile'=>$this->request['member_mobile']]);
		if ($result){
            $this->error('该手机号已存在,请更换手机号码');
        }
        		//条件验证
		$this->request['type'] = $this->request('type',2);
		$this->regCondition($this->request);
		$mibao1 = $this->request('mibao1');

        $mibao2 = $this->request('mibao2');

        if (!$mibao1 || !$mibao2){
            $this->error('请填写密保问题');
        }
		$transaction = Yii::$app->db->beginTransaction();
		try{
			if(!$model->save())
				throw new \Exception($this->model_errors($model->errors));//抛出异常
			
			$memberId = Yii::$app->db->getLastInsertID();

			if(!$this->regAfter($model,$memberId))
				throw new \Exception('后续操作失败');//抛出异常

            Yii::$app->cache->set('encrypted1'.$memberId,$mibao1,0);
           $re= Yii::$app->cache->set('encrypted2'.$memberId,$mibao2,0);

			$transaction->commit(); 
			$this->out('注册成功');    

		} catch(\Exception $e){
			$transaction->rollBack();
			$this->error($e->getMessage());
		}
	}

	//注册条件判断
	private function regCondition($params)
	{
		//推荐人判断
		if(!$invite_id = $params['invite_id']) 
			$this->error('请填写推荐人信息');

		$inviter = Member::getMemberById($invite_id);
		if(!$inviter) $this->error('推荐人不存在');

		//验证码验证
		$mobile = $this->request['member_mobile']?$this->request['member_mobile']:$this->request['member_email']; 
		$code   = $this->request['captcha'];
//		if(!SmsModel::validataVcode($mobile,$code,1)) $this->error('验证码错误或是已经过期');
 
		return true;
	}


	//注册后续操作
	private function regAfter($params,$memberId)
	{
		return true;
	}


	//用户手机号登录
	public function actionLogin()
	{

		$mobile = $this->request['mobile'];
		$member = Member::find()->where(['or' , 'member_mobile = "'.$mobile.'"' , 'member_email = "'.$mobile.'"'])->one();
		if(!$member) $this->error('用户不存在');
		if(!$member['state']) $this->error('你已经被冻结，暂不能登录');
		if($member['loginpwd']!=md5($this->request['password'])) $this->error('密码错误');

		$member->auth_key = Member::generateAuthKey();
		$flag = $member->save(false);

		if(!$flag) $this->error('更新失败');
		$this->out('登录成功',$member->toarray());

	}

	/*
		退出登录
	*/
	public function actionLogout() 
	{
		$authKey = $this->request['auth_key'];
		$member = Member::findOne(['auth_key'=>$authKey]);
		if (!$member){
		    $this->error('无须多次点击退出');
        }
		$member->auth_key = Member::generateAuthKey();

		$flag = $member->save(false);
		$this->out($flag);
	}



	/*
		* 通过验证码修改密码
		* account   ## 账号 邮箱或是手机号
		* password  密码
		* code  验证码 
		* type 1 登录密码  2交易密码
	*/
	public function actionEditPass()
	{
//		## 验证码验证
		$account = $this->request('account');
		$code   = $this->request('code');
//		if(!SmsModel::validataVcode($account,$code,1)) $this->error('验证码错误或是已经过期');
		$type = $this->request('type',1);

		$member = Member::find()->where(['id'=>$this->_memberId])->one();
		if($type == 1){
//		    if ($member->loginpwd!=md5($password)) $this->error('请输入正确的登录密码');
			$member->loginpwd = md5($this->request('new_password'));
		}else{
//            if ($member->paypwd!=md5($password)) $this->error('请输入正确的支付密码');
			$member->paypwd = md5($this->request('new_password'));
		}
		$member->update_time = time();
		$flag = $member->save(false);

		if(!$flag) $this->error('修改失败');
		$this->out('修改成功');

	}

    public function actionEncrypted(){
        $data=[
            1=>'你的生日?',
            2=>'你的性别?'
        ];
        return $this->out('密保问题',$data);
    }


}